<?php
/**
 * Created by PhpStorm.
 * User: Allen
 * Date: 2017/1/19
 * Time: 17:39
 */

namespace EWeiLink\JuPai\Services;


use EWeiLink\JuPai\Contact;
use EWeiLink\JuPai\JpCustomer;
use EWeiLink\JuPai\JuPai;
use EWeiLink\JuPai\Planner;

class CustomerRepo
{

    public function list($filter = [])
    {
        $user = request()->user();
        if (empty($user)) {
            return [];
        }

        $planner = Planner::findByUser($user)->first();
        if (is_null($planner)) {
            return [];
        }

        $unbinding = array_get($filter, 'unbinded', false);
        if ($unbinding === true) {
            $res_data = $planner->customers()
                ->whereNotIn('customerId', $planner->contacts->pluck('customerId')->all())
                ->get(['customerId', 'name', 'phone']);
        } else {
            $res_data = $planner->customers()
                ->get(['customerId', 'name', 'phone']);
        }

        return [
            'total' => $res_data->count(),
            'data' => $res_data
        ];
    }

    /**
     * 创建理财师
     * @param array $_options
     * @return Planner
     */
    public function create(array $_options) {
        $customer = new JpCustomer();
        $customer->customerId = (int) $_options['customerId'];
        $customer->plannerId = (int) $_options['plannerId'];
        $customer->name = $_options['name'];
        $customer->phone = $_options['phone'];
        $customer->save();
        return $customer;
    }

    /**
     * 创建多个钜派客户
     * @param array $_options
     */
    public function createMany(array $_options) {
        foreach ($_options as $item) {
            $this->create($item);
        }
    }


}
